package com.puman.ribbonconsumer.service;

import com.netflix.hystrix.*;
import com.puman.ribbonconsumer.model.User;
import org.springframework.web.client.RestTemplate;

public class UserGetCommand extends HystrixCommand<User> {

    //private static final HystrixCommandKey GETTER_KEY = HystrixCommandKey.Factory.asKey("CommandKey");

    private RestTemplate restTemplate;
    private long id;

    public UserGetCommand(RestTemplate restTemplate,Long id){
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("UserGroup")));
        this.restTemplate = restTemplate;
        this.id = id;
    }

    @Override
    protected User run() throws Exception {
        return restTemplate.getForObject("http://HELLO-SERVICE/users?id={1}",User.class,id);
    }

    @Override
    protected String getCacheKey() {
        return String.valueOf(id);
    }
}
